package com.wiscamp.ninechapters.panels.infrastructure.mappers;

import com.wiscamp.ninechapters.panels.infrastructure.po.GroupRolePO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public interface GroupRoleMapper extends JpaRepository<GroupRolePO, Long>, JpaSpecificationExecutor<GroupRolePO> {

    @Query(value = "select p from GroupRolePO p where p.groupId = :groupId and p.roleId = :roleId")
    GroupRolePO findByGroupAndRole(long groupId, long roleId);

    @Query(value = "select p.roleId from GroupRolePO p where p.groupId = :groupId")
    Collection<Long> findRoleIdsByGroup(long groupId);

    @Query(value = "select distinct p.roleId from GroupRolePO p where p.groupId in (:groupIds)")
    Collection<Long> findRoleIdsByGroups(Collection<Long> groupIds);
}
